{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "saved at number 100--natural images--\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "from skimage import io\n",
    "import matplotlib.pyplot as plt\n",
    "import random\n",
    "import os\n",
    "import cv2\n",
    "import numpy as np\n",
    "\n",
    "def image_to_patches(image, patch_height, patch_width):\n",
    "    # resize image so that it's dimensions are dividable by patch_height and patch_width\n",
    "    image_height = tf.cast(tf.shape(image)[0], dtype=tf.float32)\n",
    "    image_width = tf.cast(tf.shape(image)[1], dtype=tf.float32)\n",
    "    height = tf.cast((tf.ceil(image_height / patch_height)-1) * patch_height, dtype=tf.int32)\n",
    "    width = tf.cast((tf.ceil(image_width / patch_width) - 1) * patch_width, dtype=tf.int32)\n",
    "\n",
    "    num_rows = height // patch_height\n",
    "    num_cols = width // patch_width\n",
    "    # make zero-padding\n",
    "    image = tf.squeeze(tf.image.resize_image_with_crop_or_pad(image, height, width))\n",
    "\n",
    "    # get slices along the 0-th axis\n",
    "    image = tf.reshape(image, [num_rows, patch_height, width, -1])\n",
    "    # h/patch_h, w, patch_h, c\n",
    "    image = tf.transpose(image, [0, 2, 1, 3])\n",
    "    # get slices along the 1-st axis\n",
    "    # h/patch_h, w/patch_w, patch_w,patch_h, c\n",
    "    image = tf.reshape(image, [num_rows, num_cols, patch_width, patch_height, -1])\n",
    "    # num_patches, patch_w, patch_h, c\n",
    "    image = tf.reshape(image, [num_rows * num_cols, patch_width, patch_height, -1])\n",
    "    # num_patches, patch_h, patch_w, c\n",
    "    return tf.transpose(image, [0, 2, 1, 3])\n",
    "\n",
    "def get_random_n_patches(patches, k):\n",
    "    n = patches.shape[0]\n",
    "    if n < 2:\n",
    "        return patches\n",
    "    while(True):\n",
    "        if(k <= n):\n",
    "            rand = random.sample(range(n), k)\n",
    "            break\n",
    "        else:\n",
    "            k -= 3\n",
    "    return patches[rand]\n",
    "\n",
    "def patch_img_to_file(base_path, file_name, dest, label_anno):\n",
    "    global data\n",
    "    global label\n",
    "    image = io.imread(os.path.join(base_path, file_name))\n",
    "    #print(file_name)\n",
    "    tile_size = 512\n",
    "    image = tf.constant(image)\n",
    "    tiles = image_to_patches(image, tile_size, tile_size)\n",
    "    sess = tf.Session()\n",
    "    I, tiles = sess.run([image, tiles])\n",
    "#     print(I.shape)  #anh goc\n",
    "#     print(tiles.shape)\n",
    "    tiles = get_random_n_patches(tiles, 5)\n",
    "    j = 1\n",
    "    for i, tile in enumerate(tiles):\n",
    "#         tmp = os.path.splitext(file_name)\n",
    "#         new_name = tmp[0] + \"_patch\" + str(j) + tmp[1]\n",
    "#         cv2.imwrite(os.path.join(dest, new_name), tile)\n",
    "#         j+=1\n",
    "        data.append(tile)\n",
    "        label.append(label_anno)\n",
    "BASE1 = \"/home/liem/hai/recapture_classification/training_data/natural\"\n",
    "DES1 = \"/home/liem/hai/recapture_classification/training_data/natural_patch\"\n",
    "BASE2 = \"/home/liem/hai/recapture_classification/training_data/recapture\"\n",
    "DES2 = \"/home/liem/hai/recapture_classification/training_data/recapture_patch\"\n",
    "LOG0 = \"/home/liem/hai/recapture_classification/np0\"\n",
    "LOG1 = \"/home/liem/hai/recapture_classification/np1\"\n",
    "data = []\n",
    "label = []\n",
    "cnt1 = 1\n",
    "cnt2 = 1\n",
    "total = 1\n",
    "for item in os.listdir(BASE1):\n",
    "    patch_img_to_file(BASE1, item, DES1, 0)\n",
    "    if(cnt1 % 100 == 0):\n",
    "        np_data = np.asarray(data)\n",
    "        np_label = np.asarray(label)\n",
    "        name_data = \"data_natural_\" + str(cnt1)\n",
    "        name_label = \"label_natural_\" + str(cnt1)\n",
    "        np.save(os.path.join(LOG0, name_data), np_data)\n",
    "        np.save(os.path.join(LOG0, name_label), np_label)\n",
    "        data = []\n",
    "        label = []\n",
    "        print(\"saved at number \" + str(cnt1) + \"--natural images--\")\n",
    "    cnt1+=1\n",
    "    total +=1\n",
    "print(\"******************DONE-phase 1******************************\")\n",
    "data = []\n",
    "label = []\n",
    "for item in os.listdir(BASE2):\n",
    "    patch_img_to_file(BASE2, item, DES2, 1)\n",
    "    if(cnt2 % 75 == 0):\n",
    "        np_data = np.asarray(data)\n",
    "        np_label = np.asarray(label)\n",
    "        name_data = \"data_recapture_\" + str(cnt2)\n",
    "        name_label = \"label_recapture_\" + str(cnt2)\n",
    "        np.save(os.path.join(LOG1, name_data), np_data)\n",
    "        np.save(os.path.join(LOG1, name_label), np_label)\n",
    "        data = []\n",
    "        label = []\n",
    "        print(\"saved at number \" + str(cnt2) + \"--recapture images--\")\n",
    "    cnt2+=1\n",
    "    total +=1\n",
    "print(\"total_image: \" + str(total))\n",
    "# data = np.asarray(data)\n",
    "# label = np.asarray(label)\n",
    "# print(data.shape)\n",
    "# print(label.shape)\n",
    "# np.save(\"data\", data)\n",
    "# np.save(\"label\", label)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
